//TODO: License Header.

/*
This code is licensed under the BSD license.

*/
package analysis.metrics

import primitives.cluster.ClusterHead
import primitives.graph.Node
import search.util.TreeLoader2
import static Math.min

class CyclomaticComplexityMetric extends Metric{

	
	static def strongConnect(v){
		
	}
	
	static def tarjan(nodes){
		
		Idx idx = new Idx()
		
		idx.result = []
		idx.low = [:]
		idx.stack = []
		
		idx.index = 0
		idx.nodes = nodes
		nodes.each{
			visit(it,idx)
		}
		return idx.result
	}
	
	static def visit(node, idx){
		def low = idx.low
		def stack = idx.stack
		def result = idx.result
		def nodes = idx.nodes

		if(low[node] != null) return

		def num = low.size()
		low[node] = num
		def stack_pos = stack.size()
		stack.push(node)
		
		node.transitionsAsN.each{
			successor ->
			visit(successor, idx)
			low[node] = min(low[node],low[successor])
		}

		if (num == low[node]){
			def component = []
			component.addAll(stack)
			stack.clear()
			component.each{
				low[it] = nodes.size()
			}
			result.add(component)
		}

	}
	
	 double evaluate(ClusterHead tree){
		

		
		int e = tree.nodes.collect{
		    it.transitionsAsT.size()
        }.sum(0)
        
	
        int n = tree.nodes.size()

		return e - n + 2
        
	}
	

		public static void main(String[] args){
		new CyclomaticComplexityMetric().proc(args);
	}
}